'use strict';

const { Service } = require('ee-core');
const os = require("os");
const execShell = require("child_process").exec;
const loudness = require("loudness");
let shutdownShell = "shutdown -s -t 00";
let rebootShell = "shutdown -r -t 0";
if (os.platform() != "win32") {
  shutdownShell = "shutdown now";
  rebootShell = "shutdown -r now";
}


/**
 * system
 * @class
 */
class SystemService extends Service {

  constructor(ctx) {
    super(ctx);
  }
  
  /**
   * 关机
   */
  shutdown() {
	let command = execShell(shutdownShell, function (err, stdout, stderr) {
	  if (err || stderr) {
		console.log("shutdown failed" + err + stderr);
	  }
	});
	command.stdin.end();
	command.on("close", function (code) {
	  console.log("shutdown", code);
	});
  }
  
  /**
   * 重启
   */
  reboot() {
	let command = execShell(rebootShell, function (err, stdout, stderr) {
	  if (err || stderr) {
		console.log("reboot failed" + err + stderr);
	  }
	});
	command.stdin.end();
	command.on("close", function (code) {
	  console.log("reboot", code);
	});
  }
  /**
   * 获取音量
   */
  async getSound() {
	let val = await loudness.getVolume();
	return val;
  }
  
  /**
   * 设置音量
   * @param {Object} soundVal
   */
  async setSound(soundVal) {
	console.log(soundVal);
	await loudness.setVolume(soundVal);
  }
  

}

SystemService.toString = () => '[class SystemService]';
module.exports = SystemService;  